package com.example.myapplication2;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class editText3Activity extends AppCompatActivity {
    EditText editUser3;
    EditText editPass3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_text3);
        editUser3 = findViewById(R.id.editUser3);
        editPass3 = findViewById(R.id.editPass3);
        //设置文本监听
        editUser3.addTextChangedListener(new TextWatcher2(editUser3,11));
        editPass3.addTextChangedListener(new TextWatcher2(editPass3,6));
    }

    private class TextWatcher2 implements TextWatcher {
        EditText view;
        int i;
        public TextWatcher2(EditText view, int i) {
            this.view=view;
            this.i=i;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            String s = editable.toString();
            if (s!=null&&s.length()==i){
                //获取输入管理器
                InputMethodManager systemService = (InputMethodManager) editText3Activity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                //关闭键盘
                systemService.hideSoftInputFromWindow(view.getWindowToken(),0);
            }
        }
    }
}